﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using GrapeCity.ActiveReports;

namespace WebApplication16
{
    public partial class Default : System.Web.UI.Page
    {
        GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport();
        GrapeCity.ActiveReports.SectionReport rpt1 = new GrapeCity.ActiveReports.SectionReport();
        //public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 区域报表 多个ID 可用
            //SectionReport1 rpt1 = new SectionReport1();
            //for (int i = 0; i < 5; i++)
            //{
            //    id = i + 1;
            //    SectionReport1 rpt2 = new SectionReport1();
            //    rpt2.DataSource = GetDataSource();
            //    rpt2.Run();
            //    rpt1.Document.Pages.InsertRange(rpt1.Document.Pages.Count, rpt2.Document.Pages);
            //}

            //WebViewer1.Report = rpt1;
            //WebViewer1.ViewerType = GrapeCity.ActiveReports.Web.ViewerType.FlashViewer;
            #endregion

            #region 页面报表
            // 创建用于合并全部报表的主报表
            report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("temp1.rdlx")));
            // 在主报表中循环加载全部子报表
            for (int i = 1; i <= 5; i++)
            {
                // 创建 Container 控件，用户加载 Subreport 控件
                GrapeCity.ActiveReports.PageReportModel.Container container = new GrapeCity.ActiveReports.PageReportModel.Container();
                container.Name = string.Format("Container{0}", i);
                container.Width = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("1cm");
                container.Height = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("1cm");
                container.Left = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("0cm");
                container.Top = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length((1 * (i - 1)).ToString() + "cm");
                container.PageBreakAtEnd = true;
                container.PageBreakAtStart = false;

                // 创建 Subreport 控件，用户加载实际报表模板
                GrapeCity.ActiveReports.PageReportModel.Subreport subreport = new GrapeCity.ActiveReports.PageReportModel.Subreport();
                subreport.Name = string.Format("SubReport{0}", i);
                subreport.Width = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("1cm");
                subreport.Height = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("1cm");
                subreport.Left = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("0cm");
                subreport.Top = new GrapeCity.ActiveReports.Extensibility.Definition.Components.Length("0cm");
                GrapeCity.ActiveReports.PageReportModel.Parameter pid = new GrapeCity.ActiveReports.PageReportModel.Parameter();
                pid.Name = "ID";
                pid.Value = i.ToString();
                subreport.Parameters.Add(pid);
                subreport.ReportName = "RdlReport1.rdlx";
                container.ReportItems.Add(subreport);

                report.Report.Body.ReportItems.Add(container);
            }

            report.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(pageDocument_LocateDataSource);

            WebViewer1.Report = report;
            #endregion

        }

        private void pageDocument_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
        {
            if (args.DataSetName == "DataSet2")
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("aaa");
                DataRow dr = dt.NewRow();
                dr["aaa"] = "1900-01-02";
                dt.Rows.Add(dr);
                args.Data = dt;
            }
            else
            {
                args.Data = GetDataSource((int)args.Report.Parameters[0].Values[0].Value);
            }            
        }

          

        private DataView GetData(int id)
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("ID");
            dt1.Columns.Add("产品名称");
            dt1.Columns.Add("单位数量");
            dt1.Columns.Add("库存量");

            dt1.Rows.Add(1, "产品001", "1盒", "20");
            dt1.Rows.Add(2, "产品002", "1盒", "20");
            dt1.Rows.Add(3, "产品003", "1盒", "20");
            dt1.Rows.Add(4, "产品004", "1盒", "20");
            dt1.Rows.Add(5, "产品005", "1盒", "20");

            dt1.DefaultView.RowFilter = "ID=" + id;

            return dt1.DefaultView;
        }

        private DataTable GetDataSource(int id)
        {
            string connStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("/NWind_CHS.mdb"));
            string productSql = string.Format("SELECT * From 入库单 where 入库单ID = '" + id + "' order by ID ");
            DataLayer dl = new DataLayer(connStr, productSql);
            return dl.DataSetData.Tables["Products"];
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {        

            // Provide the page report you want to render.
            //GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/" + report + ".rdlx")));
            //_reportDef.Report.DataSources[0].DataSourceReference = "";
            //_reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //_reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("/Data/NWind_CHS.mdb"));
            //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
            // Create an output directory
            #region 页面报表支持
            //System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //// Provide settings for your rendering output.
            //GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings
            //excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
            //excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
            ////excelSetting.MultiSheet = false;
            //GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;
            ////Set the rendering extension and render the report.
            //GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension
            //excelRenderingExtension = new
            //GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
            //GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            //rpt.Document.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
            //Response.ContentType = "application/vnd.ms-excel";
            //Response.AddHeader("content-disposition", "inline;filename=MyExport.xls");
            //outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
            //Response.BinaryWrite(ms.ToArray());
            //Response.End();
            #endregion

            #region  区域报表        
            //GrapeCity.ActiveReports.Export.Excel.Section.XlsExport xlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
            //xlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
            //xlsExport1.Export(rpt1.Document,"C:\\Users\\Administrator\\Downloads\\XLSExpt.xlsx");
            GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
            XlsExport1.Export(report.Document, ms);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=MyExport.xlsx"));
            Response.BinaryWrite(ms.ToArray());
            Response.End();
            #endregion
        }

    

        protected void btnWord_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            //string report = "rptInvoice";
            //GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/Reports/" + report + ".rdlx")));
            //_reportDef.Report.DataSources[0].DataSourceReference = "";
            //_reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //_reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

            //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

            // Create an output directory
            System.IO.MemoryStream ms = new System.IO.MemoryStream();


            // Provide settings for your rendering output.
            GrapeCity.ActiveReports.Export.Word.Page.Settings wordSetting = new
            GrapeCity.ActiveReports.Export.Word.Page.Settings();
            wordSetting.UseMhtOutput = true;
            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = wordSetting;

            //Set the rendering extension and render the report.
            GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension wordRenderingExtension =
            new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            report.Document.Render(wordRenderingExtension, outputProvider, wordSetting);

            Response.ContentType = "application/msword";
            Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=aaa.doc"));
            outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }

        protected void btnPdf_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            //string report = "rptInvoice";
            //GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/Reports/" + report + ".rdlx")));
            //_reportDef.Report.DataSources[0].DataSourceReference = "";
            //_reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //_reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

            //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

            // Create an output directory
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            // Provide settings for your rendering output.
            GrapeCity.ActiveReports.Export.Pdf.Page.Settings pdfSetting = new
            GrapeCity.ActiveReports.Export.Pdf.Page.Settings();
            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = pdfSetting;
            //Set the rendering extension and render the report.
            GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension pdfRenderingExtension =
            new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            report.Document.Render(pdfRenderingExtension, outputProvider, pdfSetting);

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "inline;filename=aaa.pdf");
            outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }

        protected void btnHtml_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            //string report = "rptInvoice";
            //GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/Reports/" + report + ".rdlx")));
            //_reportDef.Report.DataSources[0].DataSourceReference = "";
            //_reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //_reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

            //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

            // Create an output directory
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            // Provide settings for your rendering output.
            GrapeCity.ActiveReports.Export.Html.Page.Settings htmlSetting = new
            GrapeCity.ActiveReports.Export.Html.Page.Settings();
            htmlSetting.StyleStream = false;
            htmlSetting.MhtOutput = false;
            htmlSetting.Fragment = false;
            htmlSetting.OutputTOC = true;
            htmlSetting.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Paginated;

            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = htmlSetting;

            //Set the rendering extension and render the report.
            GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension htmlRenderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            report.Document.Render(htmlRenderingExtension, outputProvider, htmlSetting);

            Response.ContentType = "text/html";
            Response.AddHeader("content-disposition", "inline;filename=客户订单.html");
            outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }

        protected void btnText_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            //string report = "rptInvoice";
            //GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/") + "Reports/" + report + ".rdlx"));
            //_reportDef.Report.DataSources[0].DataSourceReference = "";
            //_reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //_reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

            //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

            GrapeCity.ActiveReports.Export.Xml.Section.TextExport txtExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
            txtExport1.Encoding = Encoding.Unicode;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            txtExport1.Export(report.Document, ms);

            Response.ContentType = "text/plain";
            Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.txt"));
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }

        protected void btnCSV_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            //string report = "rptInvoice";
            //GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/") + "Reports/" + report + ".rdlx"));
            //_reportDef.Report.DataSources[0].DataSourceReference = "";
            //_reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //_reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

            //GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

            GrapeCity.ActiveReports.Export.Xml.Section.TextExport csvExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
            csvExport1.Encoding = Encoding.Unicode;
            csvExport1.TextDelimiter = "\t";
            csvExport1.SuppressEmptyLines = true;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            csvExport1.Export(report.Document, ms);

            Response.ContentType = "text/plain";
            Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.csv"));
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }

        protected void btnImage_Click(object sender, EventArgs e)
        {
            //string reportname = "rptInvoice";
            //GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/Reports/" + reportname + ".rdlx")));
            //report.Report.DataSources[0].DataSourceReference = "";
            //report.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            //report.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

            //GrapeCity.ActiveReports.Document.PageDocument document = new GrapeCity.ActiveReports.Document.PageDocument(report);

            GrapeCity.ActiveReports.Export.Image.Page.Settings settings = new GrapeCity.ActiveReports.Export.Image.Page.Settings();
            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.JPEG;


            settings.StartPage = 1;
            settings.EndPage = 1;

            GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension renderingExtension = new GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            report.Document.Render(renderingExtension, _provider, settings);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            _provider.GetSecondaryStreams()[0].OpenStream().CopyTo(ms);

            Response.ContentType = "image/jpg";
            Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.jpg"));
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }


    }
}